1604A - Era - CodeForces Solution


greedy *800

Please click on ads to support us..

Python Code:

import sys

input = sys.stdin.readline


def solve():
    n = int(input())
    arr = list(map(int, input().split()))
    curr = 1
    ans = 0
    for i in range(n):
        if arr[i] <= curr:
            curr += 1
        else:
            ans += (arr[i] - curr)
            curr = arr[i] + 1
    return ans


for _ in range(int(input())):
    print(solve())

C++ Code:

#include <bits/stdc++.h>
using namespace std;

typedef long long lol;

int main(){
    int t;
    cin>>t;
    while(t--){

        lol n;
        cin>>n;
        lol ar[n+5];
       
        for(int i=1;i<=n;i++)
            cin>>ar[i];

        lol ans = ar[1]-1;

        lol index = ans;

        lol j;
   

        for(int i=2;i<=n;i++){

            if((index+i)<ar[i]){

             j = ar[i] - (index+i);
                ans += j;
                index+=j;
            }
        }


        cout<<ans<<endl;

    }

}



Comments

Submit
0 Comments
More Questions

1679B - Stone Age Problem
402A - Nuts
792A - New Bus Route
221A - Little Elephant and Function
492C - Vanya and Exams
1369B - AccurateLee
892B - Wrath
999A - Mishka and Contest
727C - Guess the Array
1625C - Road Optimization
1715D - 2+ doors
267A - Subtractions
1582A - Luntik and Concerts
560A - Currency System in Geraldion
946A - Partition
1068B - LCM
1692E - Binary Deque
679A - Bear and Prime 100
488A - Giga Tower
14A - Letter
1150A - Stock Arbitraging
1552A - Subsequence Permutation
1131F - Asya And Kittens
1475F - Unusual Matrix
133B - Unary
1547A - Shortest Path with Obstacle
624A - Save Luke
1238A - Prime Subtraction
1107C - Brutality
1391B - Fix You